<template>
	<view class="merchants">
		<view class="top">
			<searchCom @searchClick="searchFun"></searchCom>
			<contentSw :swlist="conSwlist" @conswitch="conswitch"></contentSw>
		</view>
		<view class="list">
			<noData v-if="shopmList.length==0"></noData>
			<scroll-view v-else class="conList" scroll-y="true" @scrolltolower="getshopmlist">
				<shoplistCom :shoplistArr="shopmList"></shoplistCom>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import { shopmlist,shopmscan,shopmadd } from '../../api/index.js'
	import noData from '../../components/data/nodata.vue'
	import shoplistCom from '../../components/shoplist/shoplistcom.vue'
	import contentSw from '../../components/workbench/contentSw.vue'
	import searchCom from '../../components/search/searchCom.vue'
	export default {
		data() {
			return {
				conSwlist:[{name:'全部'},{name:'申请中'},{name:'绑定失败'},{name:'绑定通过'}],
				shopmList:[],
				status:0,
				page:1,
				limit:20,
				isLoad:false,//判断之前的请求是否还未结束
				isAll:false,//判断是否加载了全部数据
				searchVal:''
			}
		},
		onLoad:function(){
			this.searchFun({searchComval:''})
		},
		methods: {
			searchFun:function(data){
				this.page = 1;
				this.searchVal = data.searchComval;
				this.isAll = false;
				this.isLoad = false;
				this.getshopmlist(true);
			},
			conswitch:function(data){
				let statusObj = {
					"0":"0",//全部
					"1":"2",//申请中
					"2":"4",//绑定失败
					"3":"3"//绑定通过
				}
				this.status = statusObj[data.index];
				this.page = 1;
				this.isAll = false;
				this.isLoad = false;
				this.getshopmlist(true);
			},
			getshopmlist:function(isSw){
				let that = this;
				if(that.isLoad || that.isAll){ //假如之前的请求未完成或者列表数据已经全部加载完毕，则返回空
					console.log("请求未完成或者列表数据已经全部加载完毕")
					return ;
				}
				shopmlist({
					page:that.page,
					status:that.status,
					limit:that.limit,
					search:that.searchVal
				}).then((res) => {
					console.log(res);
					if(res.code == 0){
						if(isSw === true){//判断是否重新加载数据
							that.shopmList = that.dealList(res.data);
						}else{
							that.shopmList = that.shopmList.concat(that.dealList(res.data));
						}
						
						that.page += 1;
						that.isLoad = false;
						if(res.data.length < that.limit){
							that.isAll = true;
						}
					}else{
						this.isLoad = false;
					}
				}).catch((e) => {
					console.log(e);
				})
			},
			dealList:function(data){
				let statusObj = {
					'2':'申请中',
					'3':'绑定通过',
					'4':'绑定失败'
				}
				console.log(data)
				let list = data.map((item,index,arr)=>{
					return {
						suid:item.suid,
						shop_name:item.shop_name,
						status:item.status,
						statusText:statusObj[item.status],
						uid:item.uid,
						realname:item.realname,
						aid:item.bind_aid,
						address:item.address,
						bang:''
					};
				});
				return list
			},
			shopmaddFun:function(data){
				let that = this;
				//商圈添加
				shopmadd({
					id: data.suid,
					cmhome_shop_id: data.cmhome_shop_id
				}).then((res)=>{
					console.log(res);
					if(res.code == 0){
						//如果请求成功，刷新当前列表数据
						uni.showModal({
							title:'提示',
							content: res.msg,
							complete:function(){
								that.page = 1;
								that.isLoad = false;
								that.isAll = false;
								that.getshopmlist(true)
							}
						})
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg,
							success:function(){}
						})
					}
				}).catch((e) => {
					console.log(e);
				})
			}
		},
		onNavigationBarButtonTap() {
			let that = this;
			cm.scanCode({
				success:function(result){
					console.log(result)
					console.log("二维码扫码成功")
					shopmscan({
						url:result.code_url
					}).then((res) => {
						console.log(res)
						if(res.code == 0){
							let data = res.data;
							// status=3表示正常进入手动绑定，=2表示距离超过1.5km
							// status=3运营中心正常进入手动绑定
							if(data.status == 3){
								uni.navigateTo({ //运营中心下门店绑定商圈
									url:'../bindMerchants/bindMerchants?id='+data.suid
								})
							//status=2 表示超盟之家距离超过1.5km
							}else if(data.status == 2){
								uni.showModal({
									title:'提示',
									content: data.comment+",是否继续添加商圈?",
									success:(dataCm)=>{
										//点击了确定按钮
										if(dataCm.confirm === true){
											that.shopmaddFun(data); //添加商圈逻辑
										}
									}
								})
							//status=0 表示超盟之家在距离之内，直接通过即可
							}else if(data.status == 0){
								//如果请求成功，刷新当前列表数据
								uni.showToast({
									icon:'none',
									title:data.comment,
									success:function(){
										that.page = 1;
										that.isLoad = false;
										that.isAll = false;
										that.getshopmlist(true)
									}
								})
							}else{
								uni.showToast({
									icon:'none',
									title:data.comment,
									success:function(){}
								})
							}
						}else{
							uni.showToast({
								icon:'none',
								title:res.msg
							})
						}
						
					}).catch((e) => {
						console.log(e);
					})
				},
				fail:function(res){
					console.log(res)
					console.log("二维码扫码失败")
				}
			});
		},
		components:{
			searchCom,
			contentSw,
			shoplistCom,
			noData
		}
	}
</script>

<style lang="scss" scoped>
	.merchants{
		font-size: $uni-font-size-base;
		height: 100%;
		.top{
			height: 180rpx;
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
		}
		.list{
			height: 100%;
			
			.conList{
				height: 100%;
				background-color: #FFFFFF;
				padding-top: 200rpx;
				box-sizing: border-box;
			}
		}
	}
	.nodata{
		padding-top: 300rpx;
	}
</style>
